using Autodesk.Revit.UI;
using System.Windows.Media.Imaging;
using System;
using System.Reflection;
using System.Windows;
using System.Windows.Interop;
using System.Drawing;
using Autodesk.Revit.DB;
using Rectangle = System.Drawing.Rectangle;

using CodRevit.DockPanes;

namespace CodRevit
{
    public class App : IExternalApplication
    {
        public static UIControlledApplication? UIApp { get; private set; }
        public Result OnStartup(UIControlledApplication application)
        {
            UIApp = application;


            // 创建自定义Ribbon选项卡
            string tabName = "CodRevit";
            application.CreateRibbonTab(tabName);

            // 【1】创建Tool面板
            RibbonPanel toolPanel = application.CreateRibbonPanel(tabName, "Tool");

            // 添加简易道路按钮
            PushButtonData simpleRoadButton = new PushButtonData(
                "SimpleRoad",
                "Simple\nRoad",
                Assembly.GetExecutingAssembly().Location,
                "CodRevit.Commands.SimpleRoadCommand");
            simpleRoadButton.LargeImage = new BitmapImage(new Uri("pack://application:,,,/CodRevit;component/Resources/road.png"));
            simpleRoadButton.ToolTip = "创建易道路";
            toolPanel.AddItem(simpleRoadButton);

            // 添加删除备份文件按钮
            PushButtonData deleteBackupButton = new PushButtonData(
                "DeleteBackup",
                "Delete\nBackups",
                Assembly.GetExecutingAssembly().Location,
                "CodRevit.Commands.DeleteBackupCommand");
            deleteBackupButton.LargeImage = new BitmapImage(new Uri("pack://application:,,,/CodRevit;component/Resources/delete.png"));
            deleteBackupButton.ToolTip = "删除当前目录下的所有备份文件";
            toolPanel.AddItem(deleteBackupButton);

            // 添加散布族实例按钮
            PushButtonData scatterFamilyButton = new PushButtonData(
                "ScatterFamily",
                "Scatter\nFamily",
                Assembly.GetExecutingAssembly().Location,
                "CodRevit.Commands.ScatterFamilyCommand");
            scatterFamilyButton.LargeImage = new BitmapImage(new Uri("pack://application:,,,/CodRevit;component/Resources/scatter.png"));
            scatterFamilyButton.ToolTip = "在楼板上随机散布族实例";
            toolPanel.AddItem(scatterFamilyButton);

            // 添加曲线阵列按钮
            PushButtonData curveArrayButton = new PushButtonData(
                "CurveArray",
                "Curve\nArray",
                Assembly.GetExecutingAssembly().Location,
                "CodRevit.Commands.CurveArrayCommand");
            curveArrayButton.LargeImage = new BitmapImage(new Uri("pack://application:,,,/CodRevit;component/Resources/array.png"));
            curveArrayButton.ToolTip = "沿曲线阵列族实例";
            toolPanel.AddItem(curveArrayButton);

            // 添加材质刷按钮
            PushButtonData materialBrushData = new PushButtonData(
                "MaterialBrush",
                "Material\nBrush  ",
                Assembly.GetExecutingAssembly().Location,
                "CodRevit.Commands.MaterialBrushCommand")
            {
                LargeImage = new BitmapImage(new Uri("pack://application:,,,/CodRevit;component/Resources/materialbrush.png"))
            };
            toolPanel.AddItem(materialBrushData);

            // 添加删除过滤器按钮
            PushButtonData deleteFilterButton = new PushButtonData(
                "DeleteFilter",
                "Delete\nFilters",
                Assembly.GetExecutingAssembly().Location,
                "CodRevit.Commands.DeleteFilterCommand");
            deleteFilterButton.LargeImage = new BitmapImage(new Uri("pack://application:,,,/CodRevit;component/Resources/filter.png"));
            deleteFilterButton.ToolTip = "删除视图过滤器";
            toolPanel.AddItem(deleteFilterButton);

            // 添加模拟选择按钮
            PushButtonData simulateSelectButton = new PushButtonData("SimulateSelect", "Simulate\nSelect", Assembly.GetExecutingAssembly().Location, "CodRevit.Commands.SimulateSelectCommand");
            simulateSelectButton.LargeImage = new BitmapImage(new Uri("pack://application:,,,/CodRevit;component/Resources/simulate.png"));
            simulateSelectButton.ToolTip = "模拟选中当前视图中的所有模型";
            toolPanel.AddItem(simulateSelectButton);

            // 【2】创建Family面板
            RibbonPanel parameterPanel = application.CreateRibbonPanel(tabName, "Family");

            // 添加共享参数按钮
            PushButtonData shareParameterButton = new PushButtonData(
                "ShareParameter",
                "Share\nParameter",
                Assembly.GetExecutingAssembly().Location,
                "CodRevit.Commands.ShareParameterCommand");
            shareParameterButton.LargeImage = new BitmapImage(new Uri("pack://application:,,,/CodRevit;component/Resources/parameter.png"));
            shareParameterButton.ToolTip = "为族添加共享参数";
            parameterPanel.AddItem(shareParameterButton);

            // 添加批量参数按钮
            PushButtonData addParameterButton = new PushButtonData(
                "AddParameter",
                "Add\nParameters",
                Assembly.GetExecutingAssembly().Location,
                "CodRevit.Commands.AddParameterCommand");
            addParameterButton.LargeImage = new BitmapImage(new Uri("pack://application:,,,/CodRevit;component/Resources/parameter.png"));
            addParameterButton.ToolTip = "批量为族实例添加共享参数";
            parameterPanel.AddItem(addParameterButton);

            // 添加修改族类别按钮
            PushButtonData changeCategoryButton = new PushButtonData(
                "ChangeCategory",
                "Change\nCategory",
                Assembly.GetExecutingAssembly().Location,
                "CodRevit.Commands.ChangeCategoryCommand");
            changeCategoryButton.LargeImage = new BitmapImage(new Uri("pack://application:,,,/CodRevit;component/Resources/parameter.png"));
            changeCategoryButton.ToolTip = "批量修改族类别";
            parameterPanel.AddItem(changeCategoryButton);

            // 添加族加密按钮
            PushButtonData encryptFamilyButton = new PushButtonData(
                "EncryptFamily",
                "Encrypt\nFamily",
                Assembly.GetExecutingAssembly().Location,
                "CodRevit.Commands.EncryptFamilyCommand");
            encryptFamilyButton.LargeImage = new BitmapImage(new Uri("pack://application:,,,/CodRevit;component/Resources/lock.png"));
            encryptFamilyButton.ToolTip = "加密族参数和几何图形";
            parameterPanel.AddItem(encryptFamilyButton);

            // 添加族解密按钮
            PushButtonData decryptFamilyButton = new PushButtonData(
                "DecryptFamily",
                "Decrypt\nFamily",
                Assembly.GetExecutingAssembly().Location,
                "CodRevit.Commands.DecryptFamilyCommand");
            decryptFamilyButton.LargeImage = new BitmapImage(new Uri("pack://application:,,,/CodRevit;component/Resources/unlock.png"));
            decryptFamilyButton.ToolTip = "解密族参数和几何图形";
            parameterPanel.AddItem(decryptFamilyButton);

            // 【3】创建Export面板
            RibbonPanel exportPanel = application.CreateRibbonPanel(tabName, "Export");

            // 添加导出明细表按钮
            PushButtonData exportScheduleButton = new PushButtonData(
                "ExportSchedule",
                "Export\nSchedule",
                Assembly.GetExecutingAssembly().Location,
                "CodRevit.Commands.ExportScheduleCommand");
            exportScheduleButton.LargeImage = new BitmapImage(new Uri("pack://application:,,,/CodRevit;component/Resources/schedule.png"));
            exportScheduleButton.ToolTip = "导出明细表到Excel文件";
            exportPanel.AddItem(exportScheduleButton);

            // 添加导出族按钮
            PushButtonData exportFamilyButton = new PushButtonData(
                "ExportFamily",
                "Export\nFamily",
                Assembly.GetExecutingAssembly().Location,
                "CodRevit.Commands.ExportFamilyCommand");
            exportFamilyButton.LargeImage = new BitmapImage(new Uri("pack://application:,,,/CodRevit;component/Resources/export.png"));
            exportFamilyButton.ToolTip = "导出族文件";
            exportPanel.AddItem(exportFamilyButton);

            // 【4】创建Setting面板
            RibbonPanel settingPanel = application.CreateRibbonPanel(tabName, "Setting");
            
            // 添加SwitchTheme按钮
            PushButtonData switchThemeButton = new PushButtonData(
                "SwitchTheme",
                "Switch\nTheme",
                Assembly.GetExecutingAssembly().Location,
                "CodRevit.Commands.SwitchThemeCommand");
            switchThemeButton.LargeImage = new BitmapImage(new Uri("pack://application:,,,/CodRevit;component/Resources/theme.png"));
            switchThemeButton.ToolTip = "切换主题";
            settingPanel.AddItem(switchThemeButton);
            
            // 添加CozeBot按钮
            PushButtonData cozeBotButton = new PushButtonData(
                "CozeBot",
                "Coze\nBot",
                Assembly.GetExecutingAssembly().Location,
                "CodRevit.Commands.CozeBotCommand");
            cozeBotButton.LargeImage = new BitmapImage(new Uri("pack://application:,,,/CodRevit;component/Resources/cozebot.png"));
            cozeBotButton.ToolTip = "打开/关闭 CozeBot 对话窗口";
            settingPanel.AddItem(cozeBotButton);
            
            // 添加About按钮
            PushButtonData aboutProjectButton = new PushButtonData(
                "AboutProject",
                "About\nProject",
                Assembly.GetExecutingAssembly().Location,
                "CodRevit.Commands.AboutProjectCommand");
            aboutProjectButton.LargeImage = new BitmapImage(new Uri("pack://application:,,,/CodRevit;component/Resources/about.png"));
            aboutProjectButton.ToolTip = "查看项目信息和功能介绍";
            settingPanel.AddItem(aboutProjectButton);

            // 注册 CozeBot DockablePane
            CozeBotDockablePane.Register(application);
            return Result.Succeeded;
        }

        public Result OnShutdown(UIControlledApplication application)
        {
            return Result.Succeeded;
        }

        public static Window LeftTopWindow(ExternalCommandData commandData, Window MainWindow)
        {
            // 获取revit主应用程序窗口句柄
            IntPtr myPtr = Autodesk.Windows.ComponentManager.ApplicationWindow;
            // 将MainWindow的所有者设置为Revit应用程序窗口
            WindowInteropHelper helper = new WindowInteropHelper(MainWindow);
            helper.Owner = myPtr;
            // 获取revit活动UI视图
            UIView uiView = null;
            foreach (UIView item in commandData.Application.ActiveUIDocument.GetOpenUIViews())
            {
                if (item.ViewId == commandData.Application.ActiveUIDocument.ActiveView.Id)
                {
                    uiView = item;
                    break;
                }
            }
            // 检查UI视图是否找到
            if (uiView == null)
            {
                TaskDialog.Show("提示", "请激活当前视图再运行插件");
                return null;
            }
            // 确保窗口保持在Revit视图的前面
            WindowInteropHelper mainUI = new WindowInteropHelper(MainWindow);
            mainUI.Owner = System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle;
            // 设置窗口启动位置和位置
            MainWindow.WindowStartupLocation = WindowStartupLocation.Manual;
            Autodesk.Revit.DB.Rectangle revitRect = uiView.GetWindowRectangle();
            // 将Autodesk.Revit.DB.Rectangle转换为System.Drawing.Rectangle
            Rectangle rect = new Rectangle(
                (int)revitRect.Left,
                (int)revitRect.Top,
                (int)(revitRect.Right - revitRect.Left),
                (int)(revitRect.Bottom - revitRect.Top)
            );
            MainWindow.Top = rect.Top;
            MainWindow.Left = rect.Left;
            //// 设置窗口样式为工具窗口，这样在失去焦点时会自动隐藏
            //MainWindow.ShowInTaskbar = false;
            //MainWindow.Topmost = true;
            //// 显示窗口
            MainWindow.Show();
            return MainWindow;
        }


    }
}